import { Message } from 'element-ui'
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home',
    component: () => import('@/layout'),
    // eslint-disable-next-line no-sparse-arrays
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import('../views/home'),
      },
      {
        path: '/users',
        name: 'users',
        component: () => import('../views/users'),
      },
      ,
      {
        path: '/goodsList',
        name: 'goodsList',
        component: () => import('../views/goods/goodsList'),
      },
      {
        path: '/goodsCategory',
        name: 'goodsCategory',
        component: () => import('../views/goods/goodsCategory'),
      },
      {
        path: '/goodsAdd',
        name: 'goodsAdd',
        component: () => import('../views/goods/goodsAdd'),
      },
      {
        path: '/banner',
        name: 'banner',
        component: () => import('../views/banner'),
      },
      {
        path: '/orderlist',
        name: 'orderlist',
        component: () => import('@/views/order/orderlist'),
      },
      {
        path: '/income',
        name: 'income',
        component: () => import('@/views/order/income'),
      },
    ],
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login'),
    meta: {
      check: true,
    },
  },
  {
    path: '*',
    component: () => import('../views/404'),
    meta: {
      check: true,
    },
  },
]

const router = new VueRouter({
  routes,
})
//路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.check === true) {
    next()
  } else {
    const token = sessionStorage.getItem('token')
    if (!token) {
      Message.error('请先登录')
      next('/login')
    } else {
      next()
    }
  }
})

export default router
